package com.qianfeng.gl4study.youshare.acitivity.common;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Window;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.acitivity.store.TutorialActivity;
import com.qianfeng.gl4study.youshare.model.Category;
import com.qianfeng.gl4study.youshare.tasks.CategoryTask;
import com.qianfeng.gl4study.youshare.tasks.TaskProcessor;
import com.qianfeng.gl4study.youshare.utils.Constants;
import com.qianfeng.gl4study.youshare.view.DataStorage;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 启动扉页
 * 实现：
 *  等待5秒，若程序不是第一次运行，进入主界面。否则，启动导航页面。
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/4
 * Email:pdsfgl@live.com
 */
public class SplashActivity extends FragmentActivity implements TaskProcessor {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //不显示Title
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_splash);


        CategoryTask task = new CategoryTask(this);
        String url = "http://open.doushangshang.com/itemcats.php?id=0";
        String type = "1";
        task.execute(url, type);
    }


    private void showNextActivity() {
        //通过SharedPreference进行是否显示过导航
        SharedPreferences sharedPreferences = getSharedPreferences(Constants.SP_APP_DATA, MODE_PRIVATE);
        //是否显示过
        boolean shownTutorial = sharedPreferences.getBoolean("tutorial.shown", false);
        Intent intent;
        if(shownTutorial){
            //显示过导航，直接显示主界面
            intent = new Intent(SplashActivity.this, MainActivity.class);
        }else {
            //没显示过导航，显示导航
            intent = new Intent(SplashActivity.this, TutorialActivity.class);
        }
        SplashActivity.this.startActivity(intent);
        SplashActivity.this.finish();
    }

    /**
     * 实现相应的异步任务的处理
     * @param result
     */
    @Override
    public void processResult(JSONObject result,int flag) {
        //保存数据商品类别到一个公共的区域，然后启动其他页面
        if(flag == Constants.TASK_PRODUCT_CATEGORY) {
            if (result != null) {
                Log.d("YouShare", "result" + result + "flag:" + flag);

                try {
                    int code = result.getInt("code");
                    if(code == 1){
                        JSONObject msg = result.getJSONObject("msg");
                        JSONArray names = msg.names();

                        //存储分类的信息，通过分类ID引用到实际的分类上，只保存最上层分类，子分类包含在父分类中的sunCategory内

                       // HashMap<String ,Category> categories = new HashMap<String, Category>();

                        DataStorage dataStorage = DataStorage.getInstance();

                        if(names!=null){
                            int length = names.length();
                            for (int i = 0; i < length; i++) {
                                String string = names.getString(i);
                                //获取分类对象进行解析
                                JSONObject object = msg.getJSONObject(string);
                                Category category = new Category();
                                category.parseJSON(object);
                                //解析完成，将数据保存
                                //String id = category.getId();
                                // categories.put(id,category);
                                dataStorage.addCategory(category);

                            }

                            //Log.d("YouShare","Categories:"+categories);

                        }
                    }else {

                    }




                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }
        //显示导航页面或者主界面，结束本界面
        showNextActivity();
    }
}